<template>
  <div class='article-comments'>
    <!-- 评论列表 -->
    <van-list
      v-model='loading'
      :finished='finished'
      finished-text='没有更多了'
      @load='onLoad'
    >
      <van-cell
        v-for='item in list'
        :key='item.com_id'
      >
        <van-image
          slot='icon'
          round
          width='30'
          height='30'
          style='margin-right: 10px;'
          :src='item.aut_photo'
        />
        <span style='color: #466b9d;' slot='title'>{{ item.aut_name }}</span>
        <div slot='label'>
          <p style='color: #363636;'>{{ item.content }}</p>
          <p>
            <span style='margin-right: 10px;'>{{ item.pubdate | fromNow }}</span>
            <van-button
              size='mini'
              type='default'
              @click='replyFn(item)'
            >回复{{ item.reply_count }}
            </van-button>
          </p>
        </div>
        <van-icon
          slot='right-icon'
          :name="item.is_liking ? 'like' : 'like-o'"
          @click='likeFn(item)'
        />
      </van-cell>
    </van-list>
    <!-- /评论列表 -->

    <!-- 发布评论 -->
    <van-cell-group class='publish-wrap'>
      <van-field
        clearable
        placeholder='请输入评论内容'
        v-model.trim='content'
      >
        <van-button
          slot='button'
          size='mini'
          type='info'
          @click='publishFn'
        >发布
        </van-button>
      </van-field>
    </van-cell-group>
    <!-- /发布评论 -->
    <van-action-sheet v-model='show' title='评论回复'>
      <comment-reply
        v-if='show'
        :artId='articleId'
        :currentCommon='comment'
        @addCommonCount='addCommonCount'
      ></comment-reply>
    </van-action-sheet>
  </div>
</template>

<script>
import { addComments, getComments, likeComments, unLikeComments } from '@/api/article'
import CommentReply from '@/views/article/commentReply'

export default {
  name: 'ArticleComment',
  props: {},
  components: {
    CommentReply
  },
  computed: {
    articleId () {
      return this.$route.params.id
    }
  },
  data () {
    return {
      list: [], // 评论列表
      loading: false, // 上拉加载更多的 loading
      finished: false, // 是否加载结束
      offset: null, // 一开始加载第一页数据，所以 offset 为 null
      content: '', // 评论内容
      show: false, // 是否显示回复评论的弹窗
      comment: {} // 表示当前的回复组件, 回复的是哪个评论
    }
  },
  methods: {
    async onLoad () {
      const { data: res } = await getComments(this.articleId, this.offset)
      console.log(res)
      this.list = [...this.list, ...res.results]
      this.loading = false
      // 解决 offset 为 null 的问题
      this.offset = res.last_id

      if (res.results.length === 0) {
        this.finished = true
      }
    },
    async publishFn () {
      if (!this.content) {
        return this.$toast('请输入评论内容')
      }
      console.log('发布评论')
      // 发送请求
      const { data: res } = await addComments(this.articleId, this.content)
      console.log(res)

      // 把res.data.new_obj 添加到list最前面
      this.list.unshift(res.new_obj)
      // 提示
      this.$toast('发布成功')
      // 清空输入框
      this.content = ''
    },
    async likeFn (item) {
      // console.log(item)

      if (item.is_liking) {
        // 已经点赞了； ==>  取消点赞
        const res = await unLikeComments(item.com_id)
        console.log(res)
      } else {
        // 点赞
        const res = await likeComments(item.com_id)
        console.log(res)
      }
      this.$toast('操作成功')
      // 还要修改item.is_liking 的值，取反操作
      item.is_liking = !item.is_liking
    },
    replyFn (item) {
      console.log('当前的评论 ', item)
      // 显示 action-sheet
      this.show = true
      // 2. 更新正在回复的组件
      this.comment = item
    },
    addCommonCount () {
      // 子组件发布了评论的回复消息，需要在父组件中，让回复的数据+1
      this.comment.reply_count++
    }
  }
}
</script>

<style scoped lang='less'>
// 发表评论的区域是固定在下端的
.publish-wrap {
  position: fixed;
  left: 0;
  bottom: 0;
  width: 100%;
}

// 给发表评论区空出地方
.van-list {
  margin-bottom: 45px;
}
</style>
